<template>
  <el-card class="table-wrapper">
    <div class="table-container" style="overflow: hidden">
      <!-- 表格组建（带分页） -->
      <el-table
        ref="elTable"
        v-loading="config.loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        :data="config.dataList"
        :height="config.height || '100%'"
        :header-row-class-name="'color-header'"
        style="width: 100%"
        @row-click="rowCurrentChange"
        @selection-change="tableSelectChangeHandle"
      >
        <!-- tableSelectChangeHandle 表格row选中事件，判断是否有选项列 -->
        <el-table-column
          v-if="config.tableSelectChangeHandle"
          align="center"
          type="selection"
          width="55px"
        ></el-table-column>
        <!-- indexVisible 表格序号配置，判断是否有序号列 -->
        <el-table-column
          v-if="config.indexVisible"
          :label="config.indexVisible.label || '序号'"
          type="index"
          :align="config.indexVisible.align || 'center'"
          :width="config.indexVisible.width || '55px'"
        ></el-table-column>

        <el-table-column
          v-for="item in config.columns"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          :align="item.align || 'left'"
          :min-width="item.minWidth"
          :fixed="item.fixed"
          show-overflow-tooltip
        >
          <template #default="scope">
            <!-- 表格项 图标 -->
            <i v-if="item.icon" :style="item.iconStyle" :class="item.icon"></i>
            <!-- 彩色iconfont图标 如symbolIcon:'#naozhong' -->
            <svg v-if="item.symbolIcon" class="symbolIcon" aria-hidden="true">
              <use :xlink:href="item.symbolIcon"></use>
            </svg>
            <!-- 自定义格式化表格项  设置表格显示内容、字体、背景色、自定义cellStyle -->
            <span
              v-if="item.formatter"
              :style="{
                color: item.textColor || (item.textColorHandle && item.textColorHandle(scope.row)),
                background:
                  item.background || (item.backgroundHandle && item.backgroundHandle(scope.row)),
                ...(item.cellStyle || {})
              }"
              class="table-cell"
            >
              {{ item.formatter(scope.row) }}
            </span>
            <span v-else class="table-cell">{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>

        <!-- 操作 -->
        <el-table-column
          v-if="config.operations && operationVisible"
          label="操作"
          :fixed="config.operationFixed"
          :header-align="'left'"
          :align="'center'"
          :width="config.operationWidth || '120px'"
        >
          <template #default="scope">
            <div class="operation-btn-box">
              <template v-for="(item, index) in config.operations">
                <!-- 函数优先级低于属性 -->
                <!-- disabled是否禁用  disableHandle禁用函数 根据row数据判断是否禁用 -->
                <el-button
                  v-if="item.perm && !item.component"
                  :key="index"
                  class="operation-btn"
                  :disabled="item.disabled || (item.disableHandle && item.disableHandle(scope.row))"
                  type="text"
                  :style="{ color: item.color || '#4195F0', border: 'none' }"
                  :icon="item.icon || (item.iconHandle && item.iconHandle(scope.row))"
                  @click="item.handle(scope.row)"
                >
                  {{ item.label || (item.labelHandle && item.labelHandle(scope.row)) }}
                </el-button>

                <!-- 自定义表格项  传入row自定义展示内容 -->
                <component
                  :is="item.component"
                  v-if="item.perm && item.component"
                  :key="index"
                  :row="scope.row"
                />
              </template>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <!-- 如果有pagination配置项则有分页  基本上每个页面都有 -->
    <el-pagination
      v-if="config.pagination"
      class="pagination"
      style="margin-right: 20px"
      :current-page="config.pagination.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="config.pagination.limit"
      :total="config.pagination.total"
      :layout="config.pagination.layout || 'total, prev, pager, next, jumper'"
      @size-change="config.pagination.sizeHandle"
      @current-change="config.pagination.pageHandle"
    >
    </el-pagination>
  </el-card>
</template>

<script lang="ts">
import { ref, onMounted, computed, defineComponent, PropType } from 'vue'

export default defineComponent({
  props: {
    config: {
      type: Object as PropType<CTCardTable>,
      default() {
        return {}
      }
    }
  },
  setup(props: any) {
    // 已选row改变
    const tableSelectChangeHandle = (val: any[]) => {
      if (props.config.tableSelectChangeHandle) props.config.tableSelectChangeHandle(val)
    }

    // 点击行事件
    const rowCurrentChange = (row: any) => {
      if (props.config.rowCurrentChange) props.config.rowCurrentChange(row)
    }

    // 是否有有效的operation
    const operationVisible = computed(() => props.config.operations.some(item => item.perm))

    // el-table组件实例
    const elTable = ref<any>(null)

    // 获取默认已选项
    onMounted(() => {
      if (props.config.selectList) {
        props.config.selectList.forEach((item: any) => {
          elTable.value?.toggleRowSelection(item)
        })
      }
    })

    return {
      operationVisible,
      tableSelectChangeHandle,
      rowCurrentChange
    }
  }
})
</script>
<style lang="scss">
.el-card__body {
  height: 100%;
}
.table-wrapper {
  flex: auto;
  // flex: 1;
  margin-top: 0;
  ::v-deep .el-card__body {
    display: flex;
    flex-direction: column;
    padding-top: 0;
    padding-left: 0px;
    padding-right: 0px;
  }
  .el-card__body {
    padding: 0 !important;
  }
  .table-container {
    height: 100%;
    height: calc(100% - 60px);
    flex: auto;
    // border: none;
  }
  .operation-btn-box {
    width: 100%;
    display: flex;
    .operation-btn {
      margin-right: 12px;
      font-size: 16px !important;
    }
  }
  .pagination {
    margin: 15px 0 10px 20px;
    text-align: left;
  }
  .table-cell {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    font-size: 16px;
  }
  .el-table__row {
    td {
      padding: 0;
      height: 56px;
      line-height: 56px;
    }
  }
  .iconfont::before {
    font-size: 14px !important;
  }
}
</style>
